﻿using Exam.infranstuctrue.Dto.Request;
using Exam.infranstuctrue.Dto.Request.Exam;
using Exam.infranstuctrue.Tools;
using Exam.Infrastrucrure.Dto;
using Exam.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ServiceStack.Redis;

namespace Exam.Controllers
{
    [ApiController]
    public class ExamController : BaseController
    {

        //RedisClient RedisClient = new RedisClient("127.0.0.1", 6379);
        private readonly IExamServer _examServer;
        private readonly Session _session;

        public ExamController(IExamServer examServer, Session session)
        {
            _examServer = examServer;
            _session = session;
        }

        /// <summary>
        /// 获取所有考试
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetExam()
        {
            var uid = _session.Uid;
            var result = await _examServer.GetExam(uid);
            return Ok(result);
        }
        /// <summary>
        /// 获取课程考试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetCourseTestPage()
        {
            var uid = _session.Uid;
            var token = _session.GetToken();
            var result = await _examServer.GetCourseTestPage(uid,token);
            return Ok(result);
        }
        /// <summary>
        /// 获取培训班考试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetTrainingTestPage()
        {
            var uid = _session.Uid;
            var token = _session.GetToken();
            var result = await _examServer.GetTrainingTestPage(uid, token);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个试卷
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetEchoTestPage(string aid)
        {
            var result = await _examServer.GetEchoTestPage(aid);
            return Ok(result);
        }
        /// <summary>
        /// 未考试
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetNoExam()
        {
            var uid = _session.Uid;
            var result = await _examServer.GetNoExam(uid);
            return Ok(result);
        }
        /// <summary>
        /// 缺考
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetOverTimeExam()
        {
            var uid = _session.Uid;
            var result = await _examServer.GetOverTimeExam(uid);
            return Ok(result);
        }
        /// <summary>
        /// 已考试
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetTestPage()
        {
            var uid = _session.Uid;
            var result = await _examServer.GetTestPage(uid);
            return Ok(result);
        }
        /// <summary>
        /// 考试进行中
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetExaming()
        {
            var uid = _session.Uid;
            var result = await _examServer.GetExaming(uid);
            return Ok(result);
        }
        /// <summary>
        /// 开始考试
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> BeginExam(string aid)
        {
            var uid = _session.Uid;
            var result = await _examServer.BeginExam(uid, aid);
            return Ok(result);
        }
        /// <summary>
        /// 生成试题
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetQuestion(string aid)
        {
            var result = await _examServer.GetQuestion(aid);
            return Ok(result);
        }
        /// <summary>
        /// 考试结束
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<IActionResult> EndExam(TestPageDto dto)
        {
            dto.uid = _session.Uid;
            var result = await _examServer.EndExam(dto);
            return Ok(result);
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> ExamParticulars(string aid)
        {
            var result = await _examServer.ExamParticulars(aid);
            return Ok(result);
        }
        /// <summary>
        /// 考试排名
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> ExamRanking(string aid)
        {
            var token = _session.GetToken();
            var uid = _session.Uid;
            var result = await _examServer.ExamRanking(aid, uid,token);
            return Ok(result);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Login()
        {
            var baseUrl = Appsettings.GetVal(new string[] { "HttpBaseUrl" });
            string url = baseUrl + "api/TrainingMobile/Logins";
            var login = new LoginDto
            {
                Account = "PSQ2000",
                Password = "123456"
            };
            var s = JsonConvert.SerializeObject(login);
            var result = Http.Post(url, s);
            var res = JsonConvert.DeserializeObject<MyApiResult>(result);
            var token = JsonConvert.SerializeObject(res.data);
            var a = JsonConvert.DeserializeObject<Token>(token);
            return Ok(new
            {
                code = res.Code,
                message = res.message,
                data = a
            });

        }
    }
}
